<template>
	<view class="content">
		<view class="login">
			<image class="logo" :src="wxConfig.accountInfo.icon" />
			<view class="title">{{ wxConfig.accountInfo.nickname }}</view>
			<view class="text">首次使用需要授权才能使用哦</view>
			<button class="userlogin" @click="getUserInfoTap" :style="{ backgroundColor: template.color.temp_color }">授权登录</button>
			<button class="userlogin navigateBack" :style="{ backgroundColor: template.color.temp_color + '20', color: template.color.temp_color }" @click="navigateBack()">
				暂不登录
			</button>
		</view>
	</view>
</template>

<script>
import { getCode, login ,uploadImages,updateUserInfo} from '../../utils/api.js';
const app = getApp();
export default {
	data() {
		return {
			wxConfig: __wxConfig,
			wxCode: '',
			editInfoShow: false,
			userInfoData: {
				cname: '',
				avatar_url: ''
			}
		};
	},
	onLoad() {
		this.getWechatLogin();
	},
	onShow() {
		console.log(getApp(),456);
		// this.getWechatLogin();
	},
	methods: {
		getWechatLogin() {
			let that = this;
			uni.login({
				provider: 'weixin',
				success: function(wxs) {
					console.log(wxs,789);
					that.wxCode = wxs.code;
					console.log(that.wxCode,789);
				}
			});
		},
		getUserInfoTap() {
			let self = this;
			uni.showLoading({
				title: '正在登录中'
			});
			let identification = '';

			getCode({
				code: self.wxCode
			}).then(res => {
				identification = res.body.openid;
				uni.setStorage({
					key: '__ddminiUserOpenid',
					data: identification
				});
				
				uni.getUserProfile({
					desc: '注册身份信息验证',
					success(ues) {
						console.log(ues,999999999);
						ues.userInfo.identification = identification;
						let user = {
							nickname: ues.userInfo.nickName,
							identification: identification,
							headimgurl: ues.userInfo.avatarUrl
						};
						let postData = `wechatServerJSON=${JSON.stringify(user)}`;
						login(postData).then(wes => {
							console.log(wes.body, '888888888');
							if (wes.code == 200) {
								uni.hideLoading();
								app.globalData.user = true;
								uni.showToast({
									title: '授权登录成功',
									duration: 800
								});
								console.log(wes.body);
								uni.setStorage({
									key: 'userInfo',
									data: wes.body
								});
								setTimeout(()=>{
									uni.navigateBack();
								},500)
								
								// setTimeout(function() {
								// 	self.editInfoShow = true;
								// }, 800);
							} else {
								self.getWechatLogin();
								uni.hideLoading();
								uni.showModal({
									title: '授权失败',
									content: wes.msg,
									showCancel: false
								});
							}
						});
					},
					fail() {
						self.getWechatLogin();
						uni.showModal({
							title: '授权失败',
							content: '获取授权信息失败，请重新授权登录',
							showCancel: false
						});
					}
				});
			});
		},
		chooseavatar(img) {
			console.log(123, img);
			let self = this;
			uni.showLoading({
				title: '上传头像中',
				mask: true
			});
			let data = {
				headImg: img.detail.avatarUrl
			};
			console.log(data, '头像');
			updateUserInfo(data).then(res => {
				if (res.code === 200) {
					// console.log(res, 'pppppppppppp');
					uni.hideLoading();
					self.userInfoData.avatar_url = img.detail.avatarUrl;
				} else {
					uni.hideLoading();
					uni.showModal({
						content: res.msg,
						showCancel: false
					});
				}
			});
		},
		saveInfoFormTap(e) {
			let self = this;
			let data = e.detail.value;
			let postdta = {
				headImg: data.avatar_url,
				userName: data.cname
			};
			console.log(postdta, '用户昵称');
			if (!data.avatar_url) {
				return uni.showToast({
					title: '您还未上传头像',
					icon: 'none'
				});
			} else if (!data.cname) {
				return uni.showToast({
					title: '您还未填写昵称',
					icon: 'none'
				});
			} else {
				updateUserInfo(postdta).then(ues => {
					if (ues.code === 200) {
						self.editInfoShow = false;
						uni.hideLoading();
						uni.showToast({
							title: '资料保存成功',
							mask: true
						});
						setTimeout(function() {
							uni.navigateBack();
						}, 800);
					}
				});
			}
		},

		navigateBack() {
			uni.navigateBack();
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #fff;
}

.login {
	height: 60vh;
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;

	.logo {
		width: 120upx;
		height: 120upx;
		border-radius: 100%;
		border: 1px solid #efefef;
	}

	.title {
		color: #1f1f1f;
		font-size: 38upx;
		padding-top: 20upx;
		font-weight: normal;
	}

	.text {
		color: #666666;
		padding-top: 20upx;
		font-size: 28upx;
		padding-bottom: 60upx;
		font-weight: normal;
	}

	.userlogin {
		width: 300upx;
		height: 80upx;
		font-size: 30upx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0;
		background-color: #4e0cae;
		color: #fff;
		border-radius: 100upx;
		margin-bottom: 20upx;
		font-weight: normal;
	}

	.navigateBack {
		color: #fff;
		margin-top: 10upx;
		opacity: 0.5;
	}

	button::after {
		border: 0;
	}
}


</style>
